#delimit ;

set more 1;


*A. read data from Statistics Norway (ssb_data);

use \ssb_data\tab_flan if invkat=="A", clear;
g byr= real(faar);
g byrmo= real(fodselsmnd);
g bdate= mdy(real(substr(fodselsmnd,5,2)),15,byr);
g byte sex= kjonn=="2";

sort lopenr_person;
merge lopenr_person using \ssb_data\bosatt_2014m01d01, nokeep keep(kommnr);
tab _m;
g byte inor2013= _m==3;
ren kommnr komm2013;
drop _m;
sort lopenr_person;
merge lopenr_person using \ssb_data\bosatt_2016m01d01, nokeep keep(kommnr);
tab _m;
g byte inor2015= _m==3;
ren kommnr komm2015;
drop _m;

sort lopenr_person;
merge lopenr_person using \ssb_data\lopenr_mor_far, nokeep;
tab _m;
drop _m;

g double morid= real(lopenr_mor);
keep if morid<.;
sort morid bdate;
g tmp1= _n if morid!=morid[_n-1];
egen morrec1= mean(tmp1), by(morid);
drop tmp1;
g order= _n-morrec1+1;

g byte twin= morid==morid[_n-1] & abs(bdate-bdate[_n-1])<60;
replace twin= 1 if morid==morid[_n+1] & abs(bdate-bdate[_n+1])<60;
g byte tmp1= 1 if twin;
egen flag_twin= mean(tmp1), by(morid);
drop tmp1;

g tmp1= byr if morid!=morid[_n-1];
egen byr_oldsib= mean(tmp1), by(morid);
drop tmp1;
g tmp1= byr if morid!=morid[_n+1];
egen byr_yngsib= mean(tmp1), by(morid);
drop tmp1;
drop morrec1;

egen sibsize= count(byr), by(morid);

keep if inor2013 | inor2015;
keep if inrange(byr,1945,1995);

g lopenr_save= lopenr_person;
replace lopenr_person= lopenr_mor;

sort lopenr_person;
merge lopenr_person using \ssb_data\sivilstand_wide, nokeep keep(lopenr_ekt2013 lopenr_ekt2015 sivil2013 sivil2015);
tab _m;
drop _m;

sort lopenr_person;
merge lopenr_person using \ssb_data\tab_utd_wide, nokeep keep(bu*);
tab _m;
drop _m;
g mor_bu= bu1970 if byr<1955;
forvalues b= 1955/1995 {;
local t= `b'+15;
replace mor_bu= bu`t' if byr==`b';
};
drop bu1* bu2*;

g mor_nus1= real(substr(mor_bu,1,1));
g byte mor_hs= inrange(mor_nus1,4,8) if mor_nus1<.;
g byte mor_smcoll= inrange(mor_nus1,5,8) if mor_nus1<.;

replace lopenr_person= lopenr_far;
sort lopenr_person;
merge lopenr_person using \ssb_data\tab_utd_wide, nokeep keep(bu*);
tab _m;
drop _m;
g far_bu= bu1970 if byr<1955;
forvalues b= 1955/1995 {;
local t= `b'+15;
replace far_bu= bu`t' if byr==`b';
};
drop bu1* bu2*;

g far_nus1= real(substr(far_bu,1,1));
g byte far_hs= inrange(far_nus1,4,8) if far_nus1<.;
g byte far_smcoll= inrange(far_nus1,5,8) if far_nus1<.;

replace lopenr_person= lopenr_save;
sort lopenr_person;
merge lopenr_person using \ssb_data\tab_utd_wide, nokeep keep(bu2013 bu2015 kltrinn2013 kltrinn2015);
tab _m;
drop _m;

sort lopenr_person;
merge lopenr_person using \ssb_data\tab_pgiv_wide, nokeep keep(p_innt2010-p_innt2014);
tab _m;
drop _m;

forvalues t= 2010/2014 {;
replace p_innt`t'= 0 if p_innt`t'==. & inor2013;
};
egen inc3yr2013= rmean(p_innt2010 p_innt2011 p_innt2012);
egen inc3yr2015= rmean(p_innt2012 p_innt2013 p_innt2014);

g rank_inc3yr2013= .;
g rank_educ2013= .;

sort lopenr_person;
save data\temp, replace;

forvalues b= 1945/1995 {;
use data\temp if byr==`b' & inor2013;
cap xtile tmp1= inc3yr2013, n(100);
cap xtile tmp2= kltrinn2013 if kltrinn2013<., n(100);
keep lopenr_person tmp*;
sort lopenr_person;
save data\tmp`b', replace;
};

clear;
forvalues b= 1945/1995 {;
append using data\tmp`b';
};

sort lopenr_person;
merge lopenr_person using data\temp;
tab _m;
drop _m;

replace rank_inc3yr2013= tmp1;
replace rank_educ2013= tmp2;
drop tmp*;

g rank_inc3yr2015= .;
g rank_educ2015= .;

sort lopenr_person;
save data\temp, replace;

forvalues b= 1945/1995 {;
use data\temp if byr==`b' & inor2015;
cap xtile tmp1= inc3yr2015, n(100);
cap xtile tmp2= kltrinn2015 if kltrinn2015<., n(100);
keep lopenr_person tmp*;
sort lopenr_person;
save data\tmp`b', replace;
};

clear;
forvalues b= 1945/1995 {;
append using data\tmp`b';
};

sort lopenr_person;
merge lopenr_person using data\temp;
tab _m;
drop _m;

replace rank_inc3yr2015= tmp1;
replace rank_educ2015= tmp2;
drop tmp*;

sort lopenr_person;
save data\tmp_demo, replace;


*B create 2013 regression sample;

use \ssb_data\valgdata2013, clear;
g byte vote= stemt!="";

sort lopenr_person;
merge lopenr_person using data\tmp_demo, nokeep;
tab _m;
keep if _m==3;
drop _m;
g age= 2013-byr;
g age_oldsib= 2013-byr_oldsib;
g age_yngsib= 2013-byr_yngsib;

drop if flag_twin==1;
keep if inrange(sibsize,2,5);
keep if inrange(age,20,65);
keep if inrange(age_oldsib,20,65);
keep if inrange(age_yngsib,20,65);

g bu= bu2013;
g nus1= real(substr(bu,1,1));
g byte college= inrange(nus1,6,8) if nus1<.;

g byte par_divorced= (lopenr_far==lopenr_ekt2013 & inlist(real(sivil2013),4,5,7,8)) | (lopenr_far!=lopenr_ekt2013 & lopenr_ekt2013!="");

sort lopenr_person;
save data\sample2013, replace;


*C create 2015 regression sample;

use \ssb_data\valgdata2015, clear;
g byte vote= stemt!="";

sort lopenr_person;
merge lopenr_person using data\tmp_demo, nokeep;
tab _m;
keep if _m==3;
drop _m;
g age= 2015-byr;
g age_oldsib= 2015-byr_oldsib;
g age_yngsib= 2015-byr_yngsib;

drop if flag_twin==1;
keep if inrange(sibsize,2,5);
keep if inrange(age,20,65);
keep if inrange(age_oldsib,20,65);
keep if inrange(age_yngsib,20,65);

g bu= bu2015;
g nus1= real(substr(bu,1,1));
g byte college= inrange(nus1,6,8) if nus1<.;

g byte par_divorced= (lopenr_far==lopenr_ekt2015 & inlist(real(sivil2015),4,5,7,8)) | (lopenr_far!=lopenr_ekt2015 & lopenr_ekt2015!="");

sort lopenr_person;
save data\sample2015, replace;











